package com.sharemarking.system.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * 登录密码加密
 * @author song
 *
 */
@Component
public class PasswordEncoderImpl implements PasswordEncoder {
	
	@Autowired
    Md5ServiceImpl md5Service;

	@Override
	public String encode(CharSequence rawPassword) {
		String salt=UUID.randomUUID().toString().replaceAll("-", "");
		return md5Service.md5(rawPassword+salt)+salt;
	}

	@Override
	public boolean matches(CharSequence rawPassword, String encodedPassword) {
		String salt=encodedPassword.substring(32);
		String hash=encodedPassword.substring(0, 32);
		return md5Service.md5(rawPassword+salt).equals(hash);
	}
//	public static void main(String[] args) {
//		String rawPassword = "tfeGroup2018!@";
//		String salt=UUID.randomUUID().toString().replaceAll("-", "");
//		System.out.println( new Md5ServiceImpl().md5(rawPassword+salt)+salt);
//	}
}
